from core.models import Submission,Problem
def set_fail(rid:int):
    sub=Submission.objects.get(real_id=rid)
    sub.score=0
    sub.status="SE"
    sub.save()
from .all_judgers import HEARTBEAT_INTERVAL,RESULT_URL,JUDGER_MODE,REGISTRY
from requests import post,get
def submit_result(result:dict):
    rid=result["submission_id"]
    sub=Submission.objects.get(real_id=rid)
    sub.record=result
    sub.score=result["score"]
    sub.status=result["status"]
    sub.save()
    print(f"Run id: {rid} {sub.score} {sub.status} {result}")
    if JUDGER_MODE=="NORMAL":
        print(f"Try submit {rid} result to JM.")
        result["name"]=REGISTRY["name"]
        try:
            resp=post(RESULT_URL,json=result,timeout=HEARTBEAT_INTERVAL)
            assert resp.status_code==200
        except:
            print(f"Failed to submit result of {rid}")
            return
        print(f"Succeed to emit complete signal <{rid}>")
def get_data(subid:int):
    sub=Submission.objects.get(real_id=subid)
    pro=Problem.objects.get(problem=sub.problem)
    data={
        "submission_id":sub.real_id,
        "problem":sub.problem,
        "type":pro.type,
        "body":sub.body
    }
    return data